package cn.flexui.flexuiserver.controller.web;

import cn.dev33.satoken.stp.StpUtil;
import cn.flexui.flexuiserver.common.ResponseResultGenerator;
import cn.flexui.flexuiserver.domain.ResponseResult;
import cn.flexui.flexuiserver.domain.entity.FileInfoDetail;
import cn.flexui.flexuiserver.domain.vo.UploadFileVO;
import cn.flexui.flexuiserver.service.FileInfoDetailService;
import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author apeto
 * @create 2023/7/1 3:43 下午
 */
@Tag(name = "上传")
@Slf4j
@RequestMapping("/upload")
@RestController
public class UploadController {

    @Autowired
    private FileInfoDetailService fileInfoDetailService;
    @Autowired
    private FileStorageService fileStorageService;


    @Operation(summary = "文件上传")
    @RequestMapping("/file")
    public ResponseResult<UploadFileVO> file(@RequestParam("file") MultipartFile file, String requestId) {


        StpUtil.checkLogin();
        String fileName = file.getOriginalFilename();
        if (StrUtil.isBlank(fileName)) {
            return ResponseResultGenerator.error("文件名不能为空");
        }

        FileInfo upload = fileStorageService.of(file)
                .upload();

        String fullUrl = upload.getUrl();
        String defaultPlatform = fileStorageService.getProperties().getDefaultPlatform();
//        FileStorage fileStorage = fileStorageService.getFileStorage();
//        if (fileStorage.isSupportPresignedUrl()) {
//            GeneratePresignedUrlPretreatment generatePresignedUrlPretreatment = new GeneratePresignedUrlPretreatment();
//            generatePresignedUrlPretreatment.setFileStorageService(fileStorageService);
//            generatePresignedUrlPretreatment.setMethod(Constant.GeneratePresignedUrl.Method.GET);
//            GeneratePresignedUrlResult generatePresignedUrlResult = fileStorage.generatePresignedUrl(generatePresignedUrlPretreatment);
//            fullUrl = generatePresignedUrlResult.getUrl();
//        }


        String originalFilename = upload.getOriginalFilename();
        String filename = upload.getFilename();
        FileInfoDetail entity = new FileInfoDetail();
        entity.setPath(upload.getPath());
        entity.setFileName(filename);
        entity.setRequestId(requestId);
        entity.setPlatform(defaultPlatform);
        entity.setOriginalFileName(originalFilename);
        entity.setViewType(1);
        fileInfoDetailService.save(entity);
        UploadFileVO vo = new UploadFileVO();
        vo.setFileId(entity.getId());
        vo.setPath(upload.getPath());
        vo.setFullUrl(fullUrl);
        return ResponseResultGenerator.success(vo);

    }
}
